import java.io.*;
import java.net.*;

public class ClientThread extends Thread {
  private Socket socket = null;
  private ServerSocket clientSocket;
  private String dir;

  public ClientThread(int port, String dir) throws IOException {
    super("ClientThread");
    clientSocket = new ServerSocket(port);
    this.dir = dir;
  }

  public void run() {
    try {
      Socket socket;
      String input;
      String[] processedInput;
      BufferedReader in;
      PrintWriter out;
      File f;

      while(true) {
        socket = clientSocket.accept();
        System.out.println("Atendimento do cliente " + socket.getInetAddress().getHostAddress() + " na porta " + socket.getPort());
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream());

        input = in.readLine();
        processedInput = input.split(" ");
        switch (processedInput[0]) {
          case "add":
            break;
          case "read":
            File fi = new File("./"+dir+"/"+processedInput[1]);
            if (fi.exists()) {
              out.println("1");
              BufferedReader br = new BufferedReader(new FileReader(fi));
              String l;
              do {
                l = br.readLine();
                out.println(l);
              } while (l != null);
              out.flush();
            } else {
              out.println("0");
              out.flush();
            }
            break;
          case "create":
            f = new File("./"+dir+"/"+processedInput[1]);
            if (f.createNewFile()) {
              out.println("1");
              out.println(dir);
              out.flush();
            } else {
              out.println("0");
              out.flush();
            }
            break;
          case "remove":
            f = new File("./"+dir+"/"+processedInput[1]);
            if (f.delete()) {
              out.println("1");
              out.flush();
            } else {
              out.println("0");
              out.flush();
            }
            break;
          case "move":
            f = new File("./"+dir+"/"+processedInput[1]);
            if (f.exists()) {
              out.println("1");
              out.flush();
              sendFile(f, socket);
              f.delete();
            } else {
              System.out.println("Arquivo não existe.");
              out.println("0");
              out.flush();
            }
            break;
          case "exit":
            break;
        }
        socket.close(); // Fecha o socket.
      } 
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  public static void sendFile(File f, Socket socket) throws Exception {
      byte [] bytearray = new byte [(int) f.length()];
      FileInputStream fin = new FileInputStream(f);
      BufferedInputStream bin = new BufferedInputStream(fin);
      OutputStream os = socket.getOutputStream();
      bin.read(bytearray, 0, bytearray.length);
      System.out.println("Sending Files...");
      os.write(bytearray, 0, bytearray.length);
      os.flush();
      System.out.println("File transfer complete.");
  }
}